/**
 * Copyright 2018 orivil.com. All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found at https://mit-license.org.
 */

package actions

import (
	"gitee.com/tugoer/orivil/mux"
	"github.com/skip2/go-qrcode"
	"image/color"
	"os"
	"bytes"
	"io"
	"image/png"
	"github.com/disintegration/imaging"
	"image"
	"gitee.com/tugoer/psy-testing/code/api/tags"
)

const (
	QRCodeDir = "images/qrcode"
)

func init() {
	err := os.MkdirAll(QRCodeDir, 0777)
	if err != nil {
		panic(err)
	}
}

var QRCode = []*mux.Action{
	{
		Name:   "Get QRCode",
		Method: "GET",
		Route:  "qrcode/generate",
		Params: mux.Parameters{
			{
				In:     mux.Query,
				Schema: &getQRCode{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &getQRCode{}
			ctx.WarnParse(p)
			//hex := md5.StrToMD5(p.Url)
			//file := filepath.Join(QRCodeDir, hex+".jpg")
			//_, err := os.Stat(file)
			//if orivil.Env.Server.Debug || os.IsNotExist(err) {
				if q, err := qrcode.New(p.Url, qrcode.Medium); err != nil {
					ctx.ErrWarning(err)
				} else {
					q.ForegroundColor = color.RGBA{
						R: uint8(p.FrontR),
						G: uint8(p.FrontG),
						B: uint8(p.FrontB),
						A: uint8(p.FrontA),
					}
					q.BackgroundColor = color.RGBA{
						R: uint8(p.BackR),
						G: uint8(p.BackG),
						B: uint8(p.BackB),
						A: uint8(p.BackA),
					}
					//f, err := os.Create(file)
					//if err != nil {
					//	ctx.ErrWarning(err)
					//}
					//defer f.Close()
					buf := bytes.NewBuffer(nil)
					img := q.Image(p.Size)
					if p.Blur > 0 {
						n := imaging.Blur(img, p.Blur)
						img = imaging.Paste(img, n, image.Pt(0, 0))
					}
					err = png.Encode(buf, img)
					ctx.ErrWarning(err)
					ctx.Header().Set("Content-Type", "image/png")
					io.Copy(ctx.Response, buf)
				}
			//}
			//ctx.SendJson(map[string]string{
			//	"file": filepath.ToSlash(file),
			//})
		},
		ApiDoc: &mux.ApiDoc{
			Tags:        []string{tags.QRCode},
			Summary:     "获得二维码图片地址",
			Description: "如果图片不存在则会自动生成",
			Responses: mux.Responses{
				"200": {
					Description: "success",
					Example: map[string]string{
						"file": "images/qrcode/fsaidfjlgoip0wjfmlpijvazlaq.jpg",
					},
				},
			},
		},
	},
}

type (
	getQRCode struct {
		Url     string `required:"Url 不能为空"`
		Size    int    `required:"Size 不能为空"`
		Quality int    `required:"Quality 不能为空" num:"1<=x<=100" num-msg:"Quality 必须在1-100之间"`
		FrontR  int    `desc:"前景R通道" required:"前景R通道不能为空" num:"0<=x<=255" num-msg:"0-255之间"`
		FrontG  int    `desc:"前景G通道" required:"前景G通道不能为空" num:"0<=x<=255" num-msg:"0-255之间"`
		FrontB  int    `desc:"前景B通道" required:"前景B通道不能为空" num:"0<=x<=255" num-msg:"0-255之间"`
		FrontA  int    `desc:"前景alpha通道" required:"前景alpha通道不能为空" num:"0<=x<=255" num-msg:"0-255之间"`
		BackR   int    `desc:"背景R通道" required:"背景R通道不能为空" num:"0<=x<=255" num-msg:"0-255之间"`
		BackG   int    `desc:"背景G通道" required:"背景G通道不能为空" num:"0<=x<=255" num-msg:"0-255之间"`
		BackB   int    `desc:"背景B通道" required:"背景B通道不能为空" num:"0<=x<=255" num-msg:"0-255之间"`
		BackA   int    `desc:"背景alpha通道" required:"背景alpha通道不能为空" num:"0<=x<=255" num-msg:"0-255之间"`
		Blur float64 `desc:"模糊度" num:"0<=x<=10" num-msg:"必须在0-10之间"`
	}
)
